jQuery(function() {
var serverData = {};
serverData.key ='';
var	serverTable;
/**
 * 自动计算出差天数
 */

jQuery('#BackDate').change(function(){
	getScore();
});
jQuery("#ApplyUser").select2().trigger("change");
jQuery("#applyArea").select2().trigger("change");
jQuery('#OutStartDate').change(function(){
	getScore();
});
function getScore(){
	var a = jQuery("#BackDate").val();
	var b = jQuery("#OutStartDate").val();
	s1 = new Date(b.replace(/-/g, "/"));
	s2 = new Date(a.replace(/-/g, "/"));
	var days = s2.getTime() - s1.getTime();
	var time = parseInt(days / (1000 * 60 * 60 * 24));
	if(isNaN(time)){
		time=0;
	}
	jQuery("#OutDays").val(time);
};
//保存主表方法
function save(){
	$.springLemonDialog({
	         content: '即将保存该销售信息，是否继续？',// 确认文本
	         clickButton: function (sender, modal, index) {// 按钮事件处理
	             $(this).closeDialog(modal);
	   		if(index == 0){
				if (jQuery("#chanceRegisterForm") && !jQuery("#chanceRegisterForm").Validform({
					tiptype : 3,
					showAllError : true
				}).check(false)) {return;}
				var id = jQuery("#id").val();
				var outStartDate = jQuery("#OutStartDate").val();
				var backDate = jQuery("#BackDate").val();
				var outDays = jQuery("#OutDays").val();
				var applyUser = jQuery("#ApplyUser").val();
				var applyUserName = jQuery("#ApplyUser").find("option:selected").text();
				var applyDept = jQuery("#ApplyDept").val();
				var applyDate = jQuery("#ApplyDate").val();
				var useContent = jQuery("#useContent").val();
				var applyArea = jQuery("#applyArea").val();
				var memo = jQuery("#memo").val();
				var url = window.rootPath + "/ui/myMoney/saveMyMoney";
				var d;
				if(id==""){
					d = {
							"applyArea" : applyArea,
							"outStartDate" : outStartDate,
							"backDate" : backDate,
							"outDays" : outDays,
							//"applyUser" : applyUser,
							"applyUser": applyUser,
							"applyUserName": applyUserName,
							"applyDept" : applyDept,
							"applyDate" : applyDate,
							"useContent" : useContent,
							"memo" : memo
					};
				}else{
					d = {
							"id" : id,
							"applyArea" : applyArea,
							"outStartDate" : outStartDate,
							"backDate" : backDate,
							"outDays" : outDays,
							//"applyUser" : applyUser,
							"applyUser": applyUser,
							"applyUserName": applyUserName,
							"applyDept" : applyDept,
							"applyDate" : applyDate,
							"useContent" : useContent,
							"memo" : memo
					};
					
				}
				jQuery.ajax({
			        type: 'post',
			        url:  encodeURI(url),
			        data: JSON.stringify(d),
			        dataType: 'json',
			        contentType: 'application/json;charset=utf-8',
			        success: function (data) {
			        	jQuery("#id").val(data.id);
			        	jQuery("#saveMoneyBtn").attr("disabled","disabled");
			        }
				
			    });
	   		}
	   		}
	   		});
}
//新增保存子表方法
function saveMyMoney(url){
	var kind = jQuery("#kind").val();
	var money = jQuery("#money").val();
	var myMoneyId = jQuery("#id").val();
	//var url = window.rootPath + "/ui/myMoney/saveMyMoneyType";
	var myMoneyTypeId = jQuery("#myMoneyTypeId").val();
	var d = {
			"kind":kind,
			"money":money,
			"myMoneyId":myMoneyId
		}
	jQuery.ajax({
        type: 'post',
        url:  encodeURI(url),
        data: JSON.stringify(d),
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
        	serverData.myMoneyId = jQuery("#id").val();
        	jQuery("#myMoneyTypeId").val(data.id);
        	serverTable.draw();
        }
	
    });
	jQuery("#editReplacementModal").modal('hide');
}
//编辑保存子表方法
function updateSaveMyMoney(url){
	var kind = jQuery("#kind").val();
	var money = jQuery("#money").val();
	var myMoneyId = jQuery("#id").val();
	//var url = window.rootPath + "/ui/myMoney/saveMyMoneyType";
	var myMoneyTypeId = jQuery("#myMoneyTypeId").val();
	var d = {
			"kind":kind,
			"money":money
		}
	jQuery.ajax({
        type: 'post',
        url:  encodeURI(url),
        data: JSON.stringify(d),
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
        	serverData.myMoneyId = jQuery("#id").val();
        	jQuery("#myMoneyTypeId").val(data.id);
        	serverTable.draw();
        }
	
    });
	jQuery("#editReplacementModal").modal('hide');
}
//费用明细新增按钮
jQuery('.addMXBtn').on("click",function(){
	var id = jQuery("#id").val();
	if(id == null || id == "" || id == undefined){
		$.springLemonDialog({
	         content: '改费用未保存，是否保存',// 确认文本
	         clickButton: function (sender, modal, index) {// 按钮事件处理
	             $(this).closeDialog(modal);
	             if(index == 0){
		   				if (jQuery("#son_Form") && !jQuery("#son_Form").Validform({
		   					tiptype : 3,
		   					showAllError : true
		   				}).check(false)) {return;}
	             save();
	             jQuery("#kind").val('');
	         	jQuery("#money").val('');
	         	jQuery("#isInfo").val('');
	         	jQuery("#editReplacementModal").modal('show');
	             }
	         }
	   });
	}else{
		jQuery("#kind").val('');
     	jQuery("#money").val('');
     	jQuery("#isInfo").val('');
     	jQuery("#editReplacementModal").modal('show');
	}
});
//费用明细编辑按钮
jQuery('.updateBtn').on("click",function(){
	var selects=getSelect("#keyTable",1);
	   if(selects.length>1){
		   $.springLemonDialog({
		         content: '只能编辑一条记录',// 确认文本
		         clickButton: function (sender, modal, index) {// 按钮事件处理
		             $(this).closeDialog(modal);
		         }
		   }); 
	   }
	   else if(selects.length==0){
		   $.springLemonDialog({
		         content: '请选择要编辑的记录',// 确认文本
		         clickButton: function (sender, modal, index) {// 按钮事件处理
		             $(this).closeDialog(modal);
		         }
		   }); 
	   }
	   	jQuery("#kind").val(selects[0].kind);
		jQuery("#money").val(selects[0].money);
		jQuery("#isInfo").val(selects[0].id);
		jQuery("#editReplacementModal").modal('show');
});
//费用明细删除按钮
jQuery('.deleteBtn').on("click",function(){
	var selects=getSelect("#keyTable",1);
	   if(selects.length>1){
		   $.springLemonDialog({
		         content: '只能删除一条记录',// 确认文本
		         clickButton: function (sender, modal, index) {// 按钮事件处理
		             $(this).closeDialog(modal);
		         }
		   }); 
	   }
	   else if(selects.length==0){
		   $.springLemonDialog({
		         content: '请选择要删除的记录',// 确认文本
		         clickButton: function (sender, modal, index) {// 按钮事件处理
		             $(this).closeDialog(modal);
		         }
		   }); 
	   }
	   var id = selects[0].id
	   var url = window.rootPath + "/ui/myMoney/deleteMyMoneyType?id="+id;
	  
	   //serverData.id = id;
	   jQuery.ajax({
	        type: 'post',
	        url:  encodeURI(url),
	        data: JSON.stringify(),
	        dataType: 'json',
	        contentType: 'application/json;charset=utf-8',
	        success: function (data) {
	        	serverData.myMoneyId = jQuery("#id").val();
	        	serverTable.draw();
	        }
		
	    });
});
//费用明细弹窗中的保存按钮
jQuery('.saveOtherBtn').on("click",function(){
	var str = jQuery("#isInfo").val();
	var url;
	if(str == "" || str == null || str == undefined){//新增
		url = window.rootPath + "/ui/myMoney/saveMyMoneyType";
		saveMyMoney(url);
	}else{
		url = window.rootPath + "/ui/myMoney/updateSaveMyMoneyType?id="+jQuery("#isInfo").val();
		updateSaveMyMoney(url);
	}
	
});
//主表保存按钮
jQuery('.saveMoneyBtn').on("click",function(){
	save();
});
//费用明细数据获取
jQuery(document).ready(function () {
	
		serverData.myMoneyId = jQuery("#id").val();
	   serverTable=  jQuery('#keyTable').DataTable({
        "serverSide": true,
        "ordering": false,
        "ajax": {
            "dataType": "json",
            "type": "get",
            "cache": false,
            "url": window.rootPath+'/ui/myMoney/selectMyMoneyType',
            "data": function (d) {
                return $.extend({}, d, serverData);
            }
        },
        columns: [{ 
			        	data: null,
			            defaultContent: ""
			        },
			        {
			        	data:null,
					    render: function(data, type, full, meta){
					    	var operate ='<input type="checkbox" class="checkchild">';
					    	return operate;
							}
		            },
                  	{
		                data: 'kind'
		                
		            },
		            {
		                data: 'money'
		            },
		            {
		                data: 'applyUserName'
		            },
		            {
		            	data: 'useContent'
		            }
		          ],
        "drawCallback": function ( oSettings ) {             
            var k = 0;
            var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

            for ( var i=oSettings._iDisplayStart, iLen=total ; i<iLen ; i++ )
            {
                $('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[k] ].nTr ).text( i+1 );
                k++;
            }
                return;
        }
    });

});
});